package redisclient

import (
	"gitee.com/lsy007/redisclient/model"
	"github.com/pquerna/ffjson/ffjson"
	"time"
)

// LastSave() (timestamp int64,err error)
// ConfigGet(parameter string) (config string,err error)
// ConfigSet(parameter string, value string) (config string,err error)
// DBSize() (count int64,err error)
// Time() (date string,err error)
// Info(section string) (serverInfo string,err error)

// 返回最近一次 Redis 成功将数据保存到磁盘上的时间，以 UNIX 时间戳格式表示
func (c *RedisClient) LastSave() (timestamp int64, err error) {
	response, err = c.Request("LastSave", "Server", nil)
	return response.Int, err
}

// 获取 redis 服务的配置参数
func (c *RedisClient) ConfigGet(parameter string) (config []interface{}, err error) {
	args := &model.RedisArgsModel{Parameter: parameter}
	response, err = c.Request("ConfigGet", "Server", args)
	config = make([]interface{}, 0)
	if response.Bytes != nil {
		err = ffjson.Unmarshal(response.Bytes, &config)
	}
	return
}

// 动态地调整 Redis 服务器的配置(configuration)而无须重启
func (c *RedisClient) ConfigSet(parameter string, value string) (config string, err error) {
	args := &model.RedisArgsModel{Parameter: parameter, Value: value}
	response, err = c.Request("ConfigSet", "Server", args)
	return response.String, err
}

// 用于返回当前数据库的 key 的数量
func (c *RedisClient) DBSize() (count int64, err error) {
	response, err = c.Request("DBSize", "Server", nil)
	return response.Int, err
}

// 返回当前服务器格式化时间
func (c *RedisClient) Time() (t time.Time, err error) {
	response, err = c.Request("Time", "Server", nil)
	return response.Time, err
}

// 以一种易于理解和阅读的格式，返回关于 Redis 服务器的各种信息和统计数值
func (c *RedisClient) Info(section ...string) (serverInfo string, err error) {
	args := &model.RedisArgsModel{Section: section}
	response, err = c.Request("Info", "Server", args)
	return response.String, err
}
